﻿using MediatR;
using MS.BasicData.Infrastructure;
using MS.BasicData.Infrastructure.BaseRepository;
using MS.BasicData.Infrastructure.IBaseRepository;
using System.ComponentModel.DataAnnotations;

namespace MS.BasicData.Read.API.Applications.Products
{
    public class GetListProductRequestHandler : IRequestHandler<GetListProductRequestModel, ApiRestult>
    {
        
        private readonly IProductBaseRepository _productBaseRepository;

        private readonly IProductInfoBaseRepository _productInfoBaseRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productBaseRepository"></param>
        /// <param name="productInfoBaseRepository"></param>
        public GetListProductRequestHandler(IProductBaseRepository productBaseRepository, IProductInfoBaseRepository productInfoBaseRepository)
        {
            _productBaseRepository = productBaseRepository;
            _productInfoBaseRepository = productInfoBaseRepository;
        }

        public async Task<ApiRestult> Handle(GetListProductRequestModel request, CancellationToken cancellationToken)
        {
            ApiRestult restult = new ApiRestult
            {
                isSuc = true,
                Code = Restult.Success,
                Message = "获取溯源商品信息列表",
            };
try
            {
                //获取溯源商品信息列表
                var products = await _productBaseRepository.GetAllAsync(a => a.IsDeleted == false);

                //获取产品名称
                var productinfo = await _productInfoBaseRepository.GetAllAsync();
                
                var query = from a in products
                            join b in productinfo on a.ProductNameId equals b.ProductNameId
                            select new GetListProductResponseModel()
                            {
                                ProductId = a.ProductId,
                                ProductNameId = a.ProductNameId,
                                ProductName = b.ProductName,
                                Production = a.Production,
                                Shelflife = a.Shelflife,
                                Ionlicense = a.Ionlicense,
                                Enterprise = a.Enterprise,
                                CoverImage = a.CoverImage,
                                CreateOperatorId = a.CreateOperatorId,
                                CreateOperatorName = a.CreateOperatorName,
                                CreateDate = a.CreateDate
                            };

                restult.Data = query.ToList();
            }
            catch (Exception ex)
            {
                restult.isSuc = false;
                restult.Code = Restult.Fail;
                restult.Message = ex.Message;
            }
            return restult;
        }
    }

    public class GetListProductRequestModel : IRequest<ApiRestult>
    {

    }

    public class GetListProductResponseModel
    {
        /// <summary>
        /// 产品主键Id
        /// </summary>
        public int ProductId { get; set; }
        /// <summary>
        /// 产品名称Id
        /// </summary>
        public int ProductNameId { get; set; }
        /// <summary>
        /// 产品名称
        /// </summary>
        public string ProductName { get; set; } = string.Empty;
        /// <summary>
        /// 生产批次
        /// </summary>
        public string Production { get; set; } = string.Empty;
        /// <summary>
        /// 保质期
        /// </summary>
        public int Shelflife { get; set; }
        /// <summary>
        /// 生产许可证
        /// </summary>
        public string Ionlicense { get; set; } = string.Empty;
        /// <summary>
        /// 生产企业
        /// </summary>
        public string Enterprise { get; set; } = string.Empty;
        /// <summary>
        /// 封面图片
        /// </summary>
        public string CoverImage { get; set; } = string.Empty;
        public int CreateOperatorId { get; set; }
        public string? CreateOperatorName { get; set; }
        public DateTime CreateDate { get; set; }
    }

}
